﻿Public Class ClsDsAModOneVersion
    'represents class for manipulating with version (DsAModOneVersion object)

#Region "Attributes"
#Region "Private attributes"
    Private MeStateOfDS As GeDataSetState = GeDataSetState.kkNotInit  'state of DS
    Private MoDSaModVersion As amod2.DsAModOneVersion = Nothing 'aMod version of DS
    Private MbWasModified As Boolean = True 'indicates if DS was modified since last save    
#End Region
#End Region
#Region "Properties"
#Region "Public attributes"
    Public ReadOnly Property StateOfDS() As GeDataSetState
        Get
            Return MeStateOfDS
        End Get
    End Property
    Public ReadOnly Property DSaModVersion() As amod2.DsAModOneVersion
        Get
            Return MoDSaModVersion
        End Get
    End Property
    Public Property WasModified() As Boolean
        Get
            Return MbWasModified
        End Get
        Set(ByVal IbValue As Boolean)
            If MbWasModified <> IbValue Then
                MbWasModified = IbValue

                'raise event
                RaiseEvent MMWasChanged()
            End If
        End Set
    End Property
#End Region
#End Region
#Region "Events"
    Public Event MMWasChanged() 'event is raised after value MbWasChanged was changed
#End Region
#Region "Functions"
#Region "Open version, Save version, Close version"
    'opens DS aMod dataset
    Public Function OpenVersion(ByVal IoDsAModOneVersion As amod2.DsAModOneVersion) As GeActionResult
        Try
            If IoDsAModOneVersion IsNot Nothing AndAlso MeStateOfDS = GeDataSetState.kkNotInit Then
                MoDSaModVersion = IoDsAModOneVersion
                MeStateOfDS = GeDataSetState.kkOpened
                WasModified = False
                Return GeActionResult.kkOk
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'closes version
    Public Function CloseVersion() As GeActionResult
        Try
            If MeStateOfDS = GeDataSetState.kkOpened Then
                MeStateOfDS = GeFileState.kkNotInit
                Return GeActionResult.kkOk
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Manipulating with DS"
#Region "INSERT"
    'inserts new unit to DS
    Public Function InsertNewUnit(ByVal IsOriginalUnitName As String, ByRef OsNewUnitName As String, ByRef OsNewHumanUnitName As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            Dim LsNewUnitName As String = MakeNextUniqueUnitName(IsOriginalUnitName)
            Dim LsNewHumanUnitName As String = MakeNextUniqueHumanUnitName(IsOriginalUnitName)
            OsNewUnitName = LsNewUnitName
            OsNewHumanUnitName = LsNewHumanUnitName

            LoRow = MoDSaModVersion.UnitsTable.NewRow()
            LoRow(amod2.DsAModOneVersion.TableSysnames.Units.Columns.UnitName) = LsNewUnitName
            LoRow(amod2.DsAModOneVersion.TableSysnames.Units.Columns.HumanUnitName) = LsNewHumanUnitName

            MoDSaModVersion.UnitsTable.Rows.Add(LoRow)

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'inserts new data table to DS
    Public Function InsertNewTable(ByVal IsOriginalAppTableName As String, ByVal IsUnitName As String, ByRef OsNewAppTableName As String, ByRef OsNewHumanTableName As String) As GeActionResult
        Try
            Dim LsNewAppTableName As String = MakeNextUniqueAppTableName(IsOriginalAppTableName)
            Dim LsNewHumanTableName As String = MakeNextUniqueHumanTableName(IsOriginalAppTableName)
            OsNewAppTableName = LsNewAppTableName
            OsNewHumanTableName = LsNewHumanTableName

            If MoDSaModVersion.AddDataTable(New DataTable(OsNewAppTableName), OsNewAppTableName) IsNot Nothing Then
                Dim LoRow As DataRow = GetTableListRowByAppTableName(OsNewAppTableName)

                'settings for table in TableList
                If LoRow IsNot Nothing Then
                    LoRow(amod2.DsAModOneVersion.TableSysnames.TableList.Columns.UnitName) = IsUnitName
                    LoRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.HumanTableName) = OsNewHumanTableName
                    InsertPKcolumnToTable(OsNewAppTableName)
                Else
                    Throw New Exception
                End If
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'inserts new relationship to DS
    Public Function InsertNewRelationshipToHierarchy(ByVal IsParentAppTableName As String, ByVal IsChildTableName As String, ByVal IeRelationType As KrosLib.Data.eRelationType) As GeActionResult
        Try
            Dim LsFK As String = MakeFKForHierarchy(IsParentAppTableName)
            If InsertFKcolumnToTable(IsChildTableName, LsFK) = GeActionResult.kkOk Then

                Dim LoRows As DataRow()

                LoRows = MoDSaModVersion.HierarchyTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.TableName, IsChildTableName))
                If LoRows.Length = 1 Then
                    LoRows(0)(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ParentTable) = IsParentAppTableName
                    LoRows(0)(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ForeignKey) = LsFK '"ID_" + IsParentAppTableName
                    LoRows(0)(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.RelationType) = IeRelationType
                Else
                    Throw New Exception
                End If
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'insert new column to table
    Public Function InsertNewColumnToTable(ByVal IsAppTableName As String, ByVal IsOriginalColumnName As String, ByRef OsNewColumnName As String, ByVal IiPositionOfColumn As Integer) As GeActionResult
        Try
            Dim LoRow As DataRow
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)

            LoRow = LoTable.NewRow
            SetDefaultSettingsForNewColumn(LoRow)

            Dim LsNewColumnName As String = MakeNextUniqueColumnName(IsAppTableName, IsOriginalColumnName)
            OsNewColumnName = LsNewColumnName

            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue) = LsNewColumnName
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName) = LsNewColumnName

            LoTable.Rows.Add(LoRow)

            'MoDSaModVersion.AddRowToDataTable(LoRow, LoTable)

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'insert PK column to table
    Private Function InsertPKcolumnToTable(ByVal IsAppTableName As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)

            LoRow = LoTable.NewRow
            SetDefaultSettingsForNewColumn(LoRow)

            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.System) = KrosLib.Data.eSystemColumnType.Id
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue) = "ID"
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName) = "ID"
            LoRow(amod2.DsAModOneVersion.TableSysnames.OtherTables.Columns.UsedInView) = False


            LoTable.Rows.Add(LoRow)

            'MoDSaModVersion.AddRowToDataTable(LoRow, LoTable)

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'insert FK column to table
    Private Function InsertFKcolumnToTable(ByVal IsAppTableName As String, ByVal IsFK As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)

            LoRow = LoTable.NewRow
            SetDefaultSettingsForNewColumn(LoRow)

            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.System) = KrosLib.Data.eSystemColumnType.ForeignKey
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ResValue) = IsFK
            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName) = IsFK
            LoRow(amod2.DsAModOneVersion.TableSysnames.OtherTables.Columns.UsedInView) = False

            LoTable.Rows.Add(LoRow)

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "SELECT"
    'gets version (VID)
    Public Function GetVersionVID() As Integer
        Try
            Return CInt(MoDSaModVersion.Tables(KrosLib.Editors.xModDataSet.TableSysnames.Settings.Name).Rows(0).Item(amod2.DsAModOneVersion.TableSysnames.Settings.Columns.VID))
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return -1
        End Try
    End Function
    'gets table by AppTableName (starts without prefix)
    Public Function GetTableByAppTableName(ByVal IsAppTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataTable
        Try
            Dim LoRows As DataRow() = Nothing
            Dim LsTableName As String = ""

            'I can do it like this:
            'Return MoDSaModVersion.Tables(MakeTableNameFromAppTableName(IsAppTableName))

            'but for sure I select row from TableListTable and exract TableName directly from it
            LoRows = MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.AppTableName, IsAppTableName))

            If LoRows.Length = 1 Then
                LsTableName = LoRows(0)(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.TableName)
                'and finaly I can get DataTable from Tables of DS
                Return MoDSaModVersion.Tables(LsTableName)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets table by TableName (starts with prefix)
    Public Function GetTableByTableName(ByVal IsTableName As String) As DataTable
        Try
            Return MoDSaModVersion.Tables(IsTableName)
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets table by HumanTableName
    Public Function GetTableByHumanTableName(ByVal IsHumanTableName As String) As DataTable
        ''DOCASNE
        Return Nothing
    End Function
    'gets "column" from data table
    Private Function GetDataTableRow(ByVal IsAppTableName As String, ByVal IsColumnName As String) As DataRow
        Try
            Dim LoTable As DataTable
            Dim LoRows As DataRow()

            LoTable = GetTableByAppTableName(IsAppTableName)

            If LoTable IsNot Nothing Then
                LoRows = LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName, IsColumnName))
                If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                    Return LoRows(0)
                Else
                    Throw New Exception
                End If
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets row from TableList by AppTableName
    Public Function GetTableListRowByAppTableName(ByVal IsAppTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.AppTableName, IsAppTableName))

            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets row from TableList by HumanTableName
    Public Function GetTableListRowByHumanTableName(ByVal IsHumanTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.HumanTableName, IsHumanTableName))

            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets row from unit list table by UnitName
    Public Function GetUnitListRowByUnitName(ByVal IsUnitName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.UnitsTable.Select(String.Format("{0}='{1}'", amod2.DsAModOneVersion.TableSysnames.Units.Columns.UnitName, IsUnitName))

            If LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets row from unit list table by HumanUnitName
    Public Function GetUnitListRowByHumanUnitName(ByVal IsHumanUnitName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.UnitsTable.Select(String.Format("{0}='{1}'", amod2.DsAModOneVersion.TableSysnames.Units.Columns.HumanUnitName, IsHumanUnitName))

            If LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets row from hierarchy table by TableName
    Public Function GetHierarchyRowByTableName(ByVal IsTableName As String, Optional ByVal IbSilentMode As Boolean = False) As DataRow
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.HierarchyTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.TableName, IsTableName))

            If LoRows.Length = 1 Then
                Return LoRows(0)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , )
            Return Nothing
        End Try
    End Function
    'gets relationship name
    Public Function GetRelationshipName(ByVal IsChildTableName As String) As String
        Try
            Dim LoRow As DataRow = GetHierarchyRowByTableName(IsChildTableName)
            Return LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RelationName)

        Catch ex As Exception
            LogEventFromException(ex, , )
            Return ""
        End Try
    End Function
    'gets RTF description of table
    Public Function GetRTFdescriptionOfTable(ByVal IsAppTableName As String) As String
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.TableListTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.AppTableName, IsAppTableName))

            If LoRows IsNot Nothing AndAlso LoRows.Length = 1 Then
                Return LoRows(0)(amod2.DsAModOneVersion.TableSysnames.TableList.Columns.RTFDescription)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets RTF description of table column
    Public Function GetRTFdescriptionOfTableRow(ByVal IsAppTableName As String, ByVal IsColumnName As String) As String
        Try
            Dim LoRow As DataRow = GetDataTableRow(IsAppTableName, IsColumnName)
            If LoRow IsNot Nothing Then
                Return LoRow(amod2.DsAModOneVersion.TableSysnames.OtherTables.Columns.RTFDescription)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets RTF description of unit
    Public Function GetRTFdescriptionOfUnit(ByVal IsUnitName As String) As String
        Try
            Dim LoRow As DataRow = GetUnitListRowByUnitName(IsUnitName)
            If LoRow IsNot Nothing Then
                Return LoRow(amod2.DsAModOneVersion.TableSysnames.Units.Columns.RTFDescription)
            Else
                Throw New Exception
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets RTF description of link (relationship) between tables
    Public Function GetRTFdescriptionOfRelationship(ByVal IsChildTableName As String) As String
        Try
            Dim LoRow As DataRow = GetHierarchyRowByTableName(IsChildTableName)
            If LoRow IsNot Nothing Then
                Return LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RTFDescription)
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
    'gets class diagram data
    Public Function GetClassDiagramData() As String
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.FlowChartDataTable.Select()

            If LoRows.Length = 1 Then
                Return LoRows(0)(amod2.DsAModOneVersion.TableSysnames.FlowChart.Columns.ClassDiagram)
            Else
                Return Nothing
            End If

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return ""
        End Try
    End Function
#End Region
#Region "UPDATE"
    'update class diagram
    Public Function UpdateDiagramData(ByVal IsDiagramData As String) As GeActionResult
        Try
            Dim LoRows As DataRow()
            LoRows = MoDSaModVersion.FlowChartDataTable.Select()

            If LoRows.Length = 1 Then
                LoRows(0)(amod2.DsAModOneVersion.TableSysnames.FlowChart.Columns.ClassDiagram) = IsDiagramData
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update table RTF description
    Public Function UpdateRTFDescriptionOfTable(ByVal IsAppTableName As String, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            LoRow = GetTableListRowByAppTableName(IsAppTableName)

            If LoRow IsNot Nothing Then
                LoRow(amod2.DsAModOneVersion.TableSysnames.TableList.Columns.RTFDescription) = IsRTFtext
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update table column RTF description
    Public Function UpdateRTFDescriptionOfTableRow(ByVal IsAppTableName As String, ByVal IsColumnName As String, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetDataTableRow(IsAppTableName, IsColumnName)

            If LoRow IsNot Nothing Then
                LoRow(amod2.DsAModOneVersion.TableSysnames.OtherTables.Columns.RTFDescription) = IsRTFtext
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update unit RTF description
    Public Function UpdateRTFDescriptionOfUnit(ByVal IsUnitName As String, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetUnitListRowByUnitName(IsUnitName)
            If LoRow IsNot Nothing Then
                LoRow(amod2.DsAModOneVersion.TableSysnames.Units.Columns.RTFDescription) = IsRTFtext
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update relationship RTF description
    Public Function UpdateRTFDescriptionOfRelationship(ByVal IsChildTableName As String, ByVal IsRTFtext As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetHierarchyRowByTableName(IsChildTableName)
            If LoRow IsNot Nothing Then
                LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RTFDescription) = IsRTFtext
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update AppTableName
    Public Function UpdateAppTableName(ByVal IsAppTableName As String, ByVal IsNewAppTableName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetTableListRowByAppTableName(IsNewAppTableName, True)
            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                If MoDSaModVersion.ChangeDataTableName(IsAppTableName, IsNewAppTableName) Then
                    UpdateFKafterParentAppTableNameChanged(IsAppTableName, IsNewAppTableName)
                Else
                    Throw New Exception
                End If

            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update HumanTableName
    Public Function UpdateHumanTableName(ByVal IsAppTableName As String, ByVal IsNewHumanTableName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetTableListRowByHumanTableName(IsNewHumanTableName, True)
            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                LoRow = GetTableListRowByAppTableName(IsAppTableName)
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.TableList.Columns.HumanTableName) = IsNewHumanTableName
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update UnitName
    Public Function UpdateUnitName(ByVal IsUnitName As String, ByVal IsNewUnitName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetUnitListRowByUnitName(IsNewUnitName, True)
            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                LoRow = GetUnitListRowByUnitName(IsUnitName)
                LoRow(amod2.DsAModOneVersion.TableSysnames.Units.Columns.UnitName) = IsNewUnitName
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update HumanUnitName
    Public Function UpdateHumanUnitName(ByVal IsUnitName As String, ByVal IsNewHumanUnitName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetUnitListRowByHumanUnitName(IsNewHumanUnitName, True)
            If LoRow IsNot Nothing Then
                Return GeActionResult.kkCancel
            Else
                LoRow = GetUnitListRowByUnitName(IsUnitName)
                LoRow(amod2.DsAModOneVersion.TableSysnames.Units.Columns.HumanUnitName) = IsNewHumanUnitName
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'update relationship name
    Public Function UpdateRelationshipName(ByVal IsChildTableName As String, ByVal IsRelationshipName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetHierarchyRowByTableName(IsChildTableName)
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RelationName) = IsRelationshipName

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "DELETE"
    'deletes table from DS
    Public Function DeleteTable(ByVal IsAppTableName As String) As GeActionResult
        Try
            MoDSaModVersion.RemoveDataTable(IsAppTableName)

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'delete column from table
    Public Function DeleteColumnFromTable(ByVal IsAppTableName As String, ByVal IsColumnName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetDataTableRow(IsAppTableName, IsColumnName)
            If LoRow IsNot Nothing Then
                LoRow.Delete()
            Else
                Throw New Exception
            End If

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'deletes Unit from DS
    Public Function DeleteUnit(ByVal IsUnitName As String) As GeActionResult
        Try
            Dim LoRow As DataRow
            LoRow = GetUnitListRowByUnitName(IsUnitName)
            LoRow.Delete()

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'deletes relationship from hierarchy (it means changing data in table _Hierarchy)
    Public Function DeleteRelationShipFromHierarchy(ByVal IsParentTableName As String, ByVal IsChildTableName As String) As GeActionResult
        Try
            Dim LoRow As DataRow = GetHierarchyRowByTableName(IsChildTableName)

            LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ParentTable) = GsStringNullValue
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RTFDescription) = GsStringNullValue
            LoRow(AMod2.DsAModOneVersion.TableSysnames.Hierarchy.Columns.RelationName) = GsStringNullValue
            'foreign key and relation type is set by xMod event so I dont need to set it
            'LoRows(0)(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ForeignKey) = GsStringNullValue
            'LoRows(0)(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.RelationType) = KrosLib.Data.eRelationType.OneToMany
            DeleteColumnFromTable(IsChildTableName, MakeFKForHierarchy(IsParentTableName))

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "TRIGGERS"
    Private Function UpdateFKafterParentAppTableNameChanged(ByVal IsOldAppTableName As String, ByVal IsNewAppTableName As String) As GeActionResult
        Try
            Dim LoRows As DataRow()
            'ParentTable column is changed from xMod, so I need only to change FK
            LoRows = MoDSaModVersion.HierarchyTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ForeignKey, MakeFKForHierarchy(IsOldAppTableName)))

            For Each LoRow As DataRow In LoRows
                LoRow(KrosLib.Editors.xModDataSet.TableSysnames.Hierarchy.Columns.ForeignKey) = MakeFKForHierarchy(IsNewAppTableName)
            Next

            WasModified = True
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#End Region
#Region "Support functions"
    'sets default settings for new datatable column
    Private Function SetDefaultSettingsForNewColumn(ByRef IoRow As DataRow) As GeActionResult
        Try
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.System) = KrosLib.Data.eSystemColumnType.NotSystemColumn
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.DataType) = KrosLib.Data.eDataType.Int32
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnType) = 0 ''DOCASNE'' - pre Dana
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Length) = 20 'used when gernerating database
            IoRow(KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.Decimals) = 5 'used when gernerating database

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , )
            Return GeActionResult.kkError
        End Try
    End Function
    'makes tablename from apptablename
    Public Function MakeTableNameFromAppTableName(ByVal IsAppTableName As String) As String
        ''DOCASNE''
        Return IIf(IsAppTableName <> GsStringNullValue, "dat_" & IsAppTableName, GsStringNullValue)
    End Function
    'makes next unique AppTableName in mask "BaseNameIndex" - BaseName=string, Index=number
    Private Function MakeNextUniqueAppTableName(ByVal IsOriginalAppTableName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewAppTableName As String = ""

            If GetTableListRowByAppTableName(IsOriginalAppTableName, True) Is Nothing Then
                Return IsOriginalAppTableName
            Else
                '50x checking for unique AppTableName
                For LiI As Integer = 1 To 50
                    LsNewAppTableName = IsOriginalAppTableName & LiIndex.ToString
                    If GetTableListRowByAppTableName(LsNewAppTableName, True) Is Nothing Then Return LsNewAppTableName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalAppTableName
        Catch ex As Exception
            LogEventFromException(ex, , , )
            'on error return original name
            Return IsOriginalAppTableName
        End Try
    End Function
    'makes next unique HumanTableName in mask "BaseNameIndex" - BaseName=string, Index=number
    Private Function MakeNextUniqueHumanTableName(ByVal IsOriginalHumanTableName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewHumanTableName As String = ""

            If GetTableListRowByHumanTableName(IsOriginalHumanTableName, True) Is Nothing Then
                Return IsOriginalHumanTableName
            Else
                '50x checking for unique HumanTableName
                For LiI As Integer = 1 To 50
                    LsNewHumanTableName = IsOriginalHumanTableName & LiIndex.ToString
                    If GetTableListRowByHumanTableName(LsNewHumanTableName, True) Is Nothing Then Return LsNewHumanTableName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalHumanTableName
        Catch ex As Exception
            LogEventFromException(ex, , , )
            'on error return original name
            Return IsOriginalHumanTableName
        End Try
    End Function
    'makes next unique UnitName in mask "BaseNameIndex" - BaseName=string, Index=number
    Private Function MakeNextUniqueUnitName(ByVal IsOriginalUnitName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewUnitName As String = ""

            If GetUnitListRowByUnitName(IsOriginalUnitName, True) Is Nothing Then
                Return IsOriginalUnitName
            Else
                '50x checking for unique UnitName
                For LiI As Integer = 1 To 50
                    LsNewUnitName = IsOriginalUnitName & LiIndex.ToString
                    If GetUnitListRowByUnitName(LsNewUnitName, True) Is Nothing Then Return LsNewUnitName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalUnitName
        Catch ex As Exception
            LogEventFromException(ex, , , )
            'on error return original name
            Return IsOriginalUnitName
        End Try
    End Function
    'makes next unique HumanUnitName in mask "BaseNameIndex" - BaseName=string, Index=number
    Private Function MakeNextUniqueHumanUnitName(ByVal IsOriginalHumanUnitName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewHumanUnitName As String = ""

            If GetUnitListRowByHumanUnitName(IsOriginalHumanUnitName, True) Is Nothing Then
                Return IsOriginalHumanUnitName
            Else
                '50x checking for unique HumanUnitName
                For LiI As Integer = 1 To 50
                    LsNewHumanUnitName = IsOriginalHumanUnitName & LiIndex.ToString
                    If GetUnitListRowByHumanUnitName(LsNewHumanUnitName, True) Is Nothing Then Return LsNewHumanUnitName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalHumanUnitName
        Catch ex As Exception
            LogEventFromException(ex, , , )
            'on error return original name
            Return IsOriginalHumanUnitName
        End Try
    End Function
    'makes next unique columnname in mask "BaseNameIndex" - BaseName=string, Index=number
    Public Function MakeNextUniqueColumnName(ByVal IsAppTableName As String, ByVal IsOriginalColumnName As String) As String
        Try
            Dim LiIndex As Integer = 1
            Dim LsNewColumnName As String = ""
            Dim LoRows As DataRow()
            Dim LoTable As DataTable = GetTableByAppTableName(IsAppTableName)

            LoRows = LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName, IsOriginalColumnName))

            If LoRows.Length = 0 Then
                Return IsOriginalColumnName
            Else
                '50x checking for unique ColumnName
                For LiI As Integer = 1 To 50
                    LsNewColumnName = IsOriginalColumnName & LiIndex.ToString
                    LoRows = LoTable.Select(String.Format("{0}='{1}'", KrosLib.Editors.xModDataSet.TableSysnames.DataTable.Columns.ColumnName, LsNewColumnName))
                    If LoRows.Length = 0 Then Return LsNewColumnName
                    LiIndex += 1
                Next
            End If

            'after unsuccessful attempts return original name
            Return IsOriginalColumnName
        Catch ex As Exception
            'on error return original name
            Return IsOriginalColumnName
        End Try
    End Function
    'makes foreign key for hierarchy table
    Private Function MakeFKForHierarchy(ByVal IsParentAppTableName As String) As String
        If IsParentAppTableName = GsStringNullValue Then
            LogEvent("Nazov nadradenej tabulky je """", preto nemozem vytvorit FK!")
        End If
        Return "ID_" & IsParentAppTableName
    End Function
#End Region
#End Region

#Region "STARE"
    '' ''    'update - move column up in table _a_Columns
    '' ''    Public Function UpdateUpColumnPosition(ByVal IsAppTableName As String, ByVal IsColumnName As String, ByRef OiNewColumnPosition As Integer) As GeActionResult
    '' ''        Try
    '' ''            Dim LoRows() = GetConcreteColumn(IsAppTableName, IsColumnName)
    '' ''            Dim LiPositionOfChanging As Integer = LoRows(0).Item("Position")

    '' ''            If LiPositionOfChanging > 1 Then
    '' ''                Dim LoRowsUpColumn() = GetColumnsOfTableWithCondition(IsAppTableName, "Position=" & LiPositionOfChanging - 1)
    '' ''                LoRowsUpColumn(0)("Position") += 1
    '' ''                LoRows(0)("Position") -= 1
    '' ''                OiNewColumnPosition = LoRows(0)("Position")

    '' ''                WasModified = True
    '' ''                Return GeActionResult.kkOk
    '' ''            Else
    '' ''                Debug.Print("CHYBA!!! - UpdateUpColumnPosition")
    '' ''                Return GeActionResult.kkError
    '' ''            End If
    '' ''        Catch ex As Exception
    '' ''            Return GeActionResult.kkError
    '' ''        End Try
    '' ''    End Function
    '' ''    'update - move column down in table _a_Columns
    '' ''    Public Function UpdateDownColumnPosition(ByVal IsAppTableName As String, ByVal IsColumnName As String, ByRef OiNewColumnPosition As Integer) As GeActionResult
    '' ''        Try
    '' ''            Dim LoRows() = GetConcreteColumn(IsAppTableName, IsColumnName)
    '' ''            Dim LiPositionOfChanging As Integer = LoRows(0).Item("Position")

    '' ''            If LiPositionOfChanging < GetNumberOfColumns(IsAppTableName) Then
    '' ''                Dim LoRowsDownColumn() = GetColumnsOfTableWithCondition(IsAppTableName, "Position=" & LiPositionOfChanging + 1)
    '' ''                LoRowsDownColumn(0)("Position") -= 1
    '' ''                LoRows(0)("Position") += 1
    '' ''                OiNewColumnPosition = LoRows(0)("Position")

    '' ''                WasModified = True
    '' ''                Return GeActionResult.kkOk
    '' ''            Else
    '' ''                Debug.Print("CHYBA!!! - UpdateDownColumnPosition")
    '' ''                Return GeActionResult.kkError
    '' ''            End If
    '' ''        Catch ex As Exception
    '' ''            Return GeActionResult.kkError
    '' ''        End Try
    '' ''    End Function
#End Region

End Class